<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\widget\ajax;

use builder\helper\H5;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/**
 * 分页
 * @author cleverstone
 * @since ym2.0
 */
class LinkPager extends \yii\bootstrap4\LinkPager
{
    /**
     * @var int maximum number of page buttons that can be displayed. Defaults to 10.
     */
    public $maxButtonCount = 3;
    /**
     * @var string|bool the label for the "next" page button. Note that this will NOT be HTML-encoded.
     * If this property is false, the "next" page button will not be displayed.
     */
    public $nextPageLabel = "<span aria-hidden=\"true\" class=\"bi bi-chevron-double-right\"></span>\n<span class=\"sr-only\">Next</span>";
    /**
     * @var string|bool the text label for the "previous" page button. Note that this will NOT be HTML-encoded.
     * If this property is false, the "previous" page button will not be displayed.
     */
    public $prevPageLabel = "<span aria-hidden=\"true\" class=\"bi bi-chevron-double-left\"></span>\n<span class=\"sr-only\">Previous</span>";

    /**
     * {{@inherit}}
     */
    public $firstPageLabel = '首页';

    /**
     * {{@inherit}}
     */
    public $lastPageLabel = '尾页';

    /**
     * @var bool Hide widget when only one page exist.
     */
    public $hideOnSinglePage = false;

    /**
     * {{@inherit}}
     */
    public function run()
    {
        $pagination = $this->pagination;

        unset($this->options['id']);
        H5::addCssClass($this->options, ['widget' => 'd-flex align-items-center']);
        if ($this->registerLinkTags) {
            $this->registerLinkTags();
        }

        $options = $this->options;
        $tag = ArrayHelper::remove($options, 'tag', 'nav');
        $html = Html::beginTag($tag, $options);
        $html .= $this->renderPageButtons();
        $html .= H5::text('每页' . $pagination->pageSize . '条 / 共' . $pagination->totalCount . '条', ['class' => 'pl-2 text-muted']);
        $html .= Html::endTag($tag);

        return $html;
    }

    /**
     * Renders a page button.
     * You may override this method to customize the generation of page buttons.
     * @param string $label the text label for the button
     * @param int $page the page number
     * @param string $class the CSS class for the page button.
     * @param bool $disabled whether this page button is disabled
     * @param bool $active whether this page button is active
     * @return string the rendering result
     */
    protected function renderPageButton($label, $page, $class, $disabled, $active)
    {
        $options = $this->linkContainerOptions;
        $linkWrapTag = ArrayHelper::remove($options, 'tag', 'li');
        Html::addCssClass($options, empty($class) ? $this->pageCssClass : $class);

        $linkOptions = $this->linkOptions;
        $linkOptions['data-page'] = $page;

        if ($active) {
            Html::addCssClass($options, $this->activePageCssClass);
        }
        if ($disabled) {
            Html::addCssClass($options, $this->disabledPageCssClass);
            $disabledItemOptions = $this->disabledListItemSubTagOptions;
            $linkOptions = ArrayHelper::merge($linkOptions, $disabledItemOptions);
            $linkOptions['tabindex'] = '-1';
        }

        $current = $page + 1;
        $pageSize = $this->pagination->pageSize;
        $linkOptions['ng-click'] = "actionLinkPage({$current}, {$pageSize})";

        return Html::tag($linkWrapTag, Html::a($label, 'javascript:;', $linkOptions), $options);
    }
}